home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: qsort help
- Date: Wed, 03 Jan 96 20:33:05 GMT
- Organization: none
- Message-ID: <820701185snz@genesis.demon.co.uk>
- References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu> <4cdomq$jgt@dub-news-svc-5.compuserve.com> <4ce4oi$hsd@news.iag.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ce4oi$hsd@news.iag.net> jatmon@iag.net "John R Buchan" writes:
-
- >If this function is to be passed to qsort, it needs to be something like:
- >
- >int compar( const void *a, const void *b)
- >{
- > const int *c = a, *d = b;
- > return *c - *d;
- >}
-
- That's a trick that only works when you know something about the ranges
- of the elements involved, for instance an implementation might use it
- in the strcmp() library function when it knows that (sizeof(int) > 1),
- or more precisely that int and char have suitable ranges. In the general
- case *c - *d can overflow resulting in undefined behaviour.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-